//
//  MIDIcommController.m
//  迷笛
//
//  Created by . on 2018/9/20.
//  Copyright © 2018年 🍃🌺🌺🍃. All rights reserved.
//

#import "MIDIcommController.h"
#import "MIDIcommCell.h"
#import "MIDIcommmodel.h"
#import "XHInputView.h"

@interface MIDIcommController ()<UITableViewDelegate,UITableViewDataSource,XHInputViewDelagete>
@property(nonatomic,strong)UITableView * Hottable;
@property(nonatomic,strong)UIView * backview;
@property(nonatomic,strong)UIButton * love;
@property(nonatomic,strong)UILabel * lovetext;
@property(nonatomic,strong)UIButton * comm;
@property(nonatomic,strong)UILabel * commtext;
@property(nonatomic,copy)NSString * apptoken;
@property(nonatomic,copy)NSString * commapi;

@property(nonatomic,copy)NSString * textStr;

@property(nonatomic,copy)NSString *releaseapi;

@property(nonatomic,strong)NSMutableArray * commArr;

@property(nonatomic,copy)NSString * praiseapi;

@property(nonatomic,copy)NSString *detailsapi;

@end

@implementation MIDIcommController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _apptoken = [user objectForKey:@"APPToken"];
    

    
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(Block:)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:@"后退"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    [self homeUI];

}
-(void)commRequest
{
    NSString * page = @"1";
    
    self.commapi = [NSString stringWithFormat:@"%@circle/dynamic/comment/%@?page=%@&token=%@",MIDIApiRoot,self.ID,page,_apptoken];
    
    [EdHttp get:self.commapi params:nil progress:^(id downloadProgress)
    {
        
    }
        success:^(id responseObject)
    {
        NSLog(@"获取评论%@",responseObject);
        NSArray *arr4 = responseObject[@"data"];
    
        for (NSDictionary *dic4 in arr4)
        {
            NSDictionary * user;

            NSString * content;
            NSString * date;
            NSString * name;
            NSString * avatar;
            if (![dic4[@"content"] isKindOfClass:[NSNull class]])
            {
                content = dic4[@"content"];
            }
            if (![dic4[@"date"] isKindOfClass:[NSNull class]])
            {
                date = dic4[@"date"];
            }
            
            if (![dic4[@"user"] isKindOfClass:[NSNull class]])
            {
                user = dic4[@"user"];
            }
            if (![user[@"nickname"] isKindOfClass:[NSNull class]])
            {
                name = user[@"nickname"];
            }
            if (![user[@"avatar"] isKindOfClass:[NSNull class]])
            {
                avatar = user[@"avatar"];
            }
           MIDIcommmodel  * mact =[MIDIcommmodel new];
            mact.nickname = name;
            mact.content = content;
            mact.avatar = avatar;
            mact.date = date;
            [self.commArr addObject:mact];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.Hottable reloadData];
        });


    }
        failure:^(NSError *error)
    {
        NSLog(@"失败");
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.commArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    MIDIcommCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell =[[MIDIcommCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.backgroundColor =[UIColor colorWithHexString:@"#1B212F"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        [_Hottable setSeparatorColor : [UIColor blackColor]];

    }
    
    MIDIcommmodel * mm1 = self.commArr[indexPath.row];
    [cell.headimage sd_setBackgroundImageWithURL:mm1.avatar forState:0];
    cell.name.text = mm1.nickname;
    cell.count.text = mm1.content;
    cell.time.text = mm1.date;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

// 返回上一级
-(void)Block:(UIButton * )sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;//Bar的模糊效果，默认为YES
    
    [self commRequest];
    self.commArr = [NSMutableArray array];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#171717"];
    self.title = @"评论";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"],NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:WidthScale(17)]}];
    //    去掉导航黑线
    UIImage *img = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}
-(UITableView *)Hottable
{
    if (!_Hottable)
    {
        _Hottable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, kDevice_Is_iPhoneX?HeighX(700):HeightScale(560)) style:UITableViewStyleGrouped];
        _Hottable.dataSource = self;
        _Hottable.delegate = self;
        _Hottable.backgroundColor = [UIColor colorWithHexString:@"171717"];
    }
    return _Hottable;
}
-(void)homeUI
{
    [self.view addSubview:self.Hottable];
    [self.view addSubview:self.backview];
    [self.backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(kDevice_Is_iPhoneX?HeighX(-34):HeightScale(0));
        make.centerX.equalTo(self.view);
        make.width.mas_offset(self.view.frame.size.width);
        make.height.mas_offset(kDevice_Is_iPhoneX?HeighX(49):HeightScale(49));
    }];
    [self.backview addSubview:self.love];
    [self.love mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backview);
        make.left.equalTo(self.backview).mas_offset(WidthScale(60));
        make.width.mas_offset(WidthScale(30));
        make.height.mas_offset(kDevice_Is_iPhoneX?HeighX(30):HeightScale(30));
    }];
    [self.backview addSubview:self.lovetext];
    [self.lovetext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.love);
        make.left.equalTo(self.love.mas_right).mas_offset(WidthScale(5));
        [self.lovetext setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    [self.backview addSubview:self.comm];
    [self.comm mas_makeConstraints:^(MASConstraintMaker *make)
     {
        make.centerY.equalTo(self.backview);
        make.right.mas_offset(WidthScale(-90));
        make.width.mas_offset(WidthScale(30));
        make.height.mas_offset(kDevice_Is_iPhoneX?HeighX(30):HeightScale(30));
    }];
    [self.backview addSubview:self.commtext];
    [self.commtext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.comm);
        make.left.equalTo(self.comm.mas_right).mas_offset(WidthScale(5));
        [self.commtext setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
}

-(UIView *)backview
{
    if (!_backview)
    {
        _backview =[[UIView alloc]init];
        _backview.backgroundColor =[UIColor colorWithHexString:@"171717"];
    }
    return _backview;
}
-(UIButton *)love
{
    if (!_love)
    {
        _love =[[UIButton alloc]init];
        if ([self.praiseID isEqualToNumber:@1])
        {
            [_love setBackgroundImage:[UIImage imageNamed:@"xuanzhong-xin"] forState:0];
            [_love addTarget:self action:@selector(praiseBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            [_love setBackgroundImage:[UIImage imageNamed:@"xin"] forState:0];
            [_love addTarget:self action:@selector(praisebtn:) forControlEvents:UIControlEventTouchUpInside];

        }
    }
    return _love;
}
-(void)praiseBtn:(UIButton * )sender
{
    
    self.praiseapi = [NSString stringWithFormat:@"%@circle/dynamic/collect/%@?token=%@",MIDIApiRoot,self.ID,_apptoken];
    NSLog(@"two点赞%@",self.praiseapi);
    
    if (sender.selected == YES)
    {
        

        [EdHttp get:self.praiseapi params:nil progress:^(id downloadProgress)
         {
             
         }
            success:^(id responseObject)
         {
             if ([[responseObject objectForKey:@"code"] isEqualToNumber:@200])
             {
            
                     sender.selected=!sender.selected;//每次点击都改变按钮的状态
                     
                     [sender setBackgroundImage:[UIImage imageNamed:@"xuanzhong-xin"] forState:0];
                     [SVProgressHUD showSuccessWithStatus:@"点赞成功"];
                     [SVProgressHUD dismissWithDelay:1.0];
                     
                     NSLog(@"点赞成功%@",responseObject);
                     
                 
                
             }
         }
            failure:^(NSError *error)
         {
             NSLog(@"失败");
         }];
        NSLog(@"已关注");
        
    }
    else
    {
        
        [EdHttp get:self.praiseapi params:nil progress:^(id downloadProgress)
         {
             
         }
            success:^(id responseObject)
         {
             
             
             if ([[responseObject objectForKey:@"code"] isEqualToNumber:@200])
             {

               
                 sender.selected=!sender.selected;//每次点击都改变按钮的状态

                 [sender setBackgroundImage:[UIImage imageNamed:@"xin"] forState:0];
                 [SVProgressHUD showSuccessWithStatus:@"取消成功"];
                 [SVProgressHUD dismissWithDelay:1.0];
                 
                 NSLog(@"取消成功%@",responseObject);
                
         
             }

             
         }
            failure:^(NSError *error)
         {
             NSLog(@"失败");
         }];
             
         }
        
}
-(void)praisebtn:(UIButton * )sender
{
    self.praiseapi = [NSString stringWithFormat:@"%@circle/dynamic/collect/%@?token=%@",MIDIApiRoot,self.ID,_apptoken];
    NSLog(@"two点赞%@",self.praiseapi);
    
    if (sender.selected == YES)
    {
        
        
        [EdHttp get:self.praiseapi params:nil progress:^(id downloadProgress)
         {
             
         }
            success:^(id responseObject)
         {
             if ([[responseObject objectForKey:@"code"] isEqualToNumber:@200])
             {
                 
                
                 sender.selected=!sender.selected;//每次点击都改变按钮的状态
                 
                 [sender setBackgroundImage:[UIImage imageNamed:@"xin"] forState:0];
                 [SVProgressHUD showSuccessWithStatus:@"取消成功"];
                 [SVProgressHUD dismissWithDelay:1.0];
                 
                 NSLog(@"取消成功%@",responseObject);
                 
                 
             }
         }
            failure:^(NSError *error)
         {
             NSLog(@"失败");
         }];
        NSLog(@"已关注");
        
    }
    else
    {
        
        [EdHttp get:self.praiseapi params:nil progress:^(id downloadProgress)
         {
             
         }
            success:^(id responseObject)
         {
             
             
             if ([[responseObject objectForKey:@"code"] isEqualToNumber:@200])
             {
                 
                 sender.selected=!sender.selected;//每次点击都改变按钮的状态
                 
                 [sender setBackgroundImage:[UIImage imageNamed:@"xuanzhong-xin"] forState:0];
                 [SVProgressHUD showSuccessWithStatus:@"点赞成功"];
                 [SVProgressHUD dismissWithDelay:1.0];
                 
                 NSLog(@"点赞成功%@",responseObject);
                 
                 
                 
                 
                 
             }
             
             
         }
            failure:^(NSError *error)
         {
             NSLog(@"失败");
         }];
        
    }
}


-(UILabel *)lovetext
{
    if (!_lovetext)
    {
        _lovetext = [[UILabel alloc]init];
        _lovetext.text = @"赞";
        _lovetext.font =[UIFont systemFontOfSize:WidthScale(15)];
        _lovetext.textColor =[UIColor colorWithHexString:@"#FFFFFF"];
    }
    return _lovetext;
}
-(UIButton *)comm
{
    if (!_comm)
    {
        _comm =[[UIButton alloc]init];
        [_comm setBackgroundImage:[UIImage imageNamed:@"pinglun"] forState:0];
        [_comm addTarget:self action:@selector(commBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comm;
}
-(UILabel *)commtext
{
    if (!_commtext)
    {
        _commtext = [[UILabel alloc]init];
        _commtext.text = @"评论";
        _commtext.font =[UIFont systemFontOfSize:WidthScale(15)];
        _commtext.textColor =[UIColor colorWithHexString:@"#FFFFFF"];
    }
    return _commtext;
}
-(void)commBtn
{
    [self showXHInputViewWithStyle:InputViewStyleLarge];//显示样式二

}
//输入框第三方 评论
-(void)showXHInputViewWithStyle:(InputViewStyle)style
{
    
    [XHInputView showWithStyle:style configurationBlock:^(XHInputView *inputView) {
        /** 请在此block中设置inputView属性 */
        
        /** 代理 */
        inputView.delegate = self;
        
        /** 占位符文字 */
        inputView.placeholder = @"请输入评论文字...";
        /** 设置最大输入字数 */
        inputView.maxCount = 200;
        /** 输入框颜色 */
        inputView.textViewBackgroundColor = [UIColor groupTableViewBackgroundColor];
        
        /** 更多属性设置,详见XHInputView.h文件 */
        
    } sendBlock:^BOOL(NSString *text) {
        if(text.length){
            //            NSLog(@"输入的信息为:%@",text);
            self.textStr = text;
            [self releaseRequest];
            return YES;//return YES,收起键盘
        }else{
            NSLog(@"显示提示框-请输入要评论的的内容");
            return NO;//return NO,不收键盘
        }
    }];
    
}
-(void)releaseRequest
{
    
    NSString * touserid = @"0";
    
    NSDictionary * dic = @{@"dynamic_id":self.ID,@"content":self.textStr,@"to_user_id":touserid,@"token":self.apptoken};

    self.releaseapi =[NSString stringWithFormat:@"%@circle/dynamic/comment",MIDIApiRoot];
    
    [EdHttp post:self.releaseapi params:dic progress:^(id uploadProgress)
     {
        
    } success:^(id responseObject)
    {
        NSLog(@"评论内容%@",responseObject);
    }
         failure:^(NSError *error)
    {
        NSLog(@"失败");
    }];
    
}

-(void)detailsRequest
{
    NSString * page = @"1";
    self.detailsapi = [NSString stringWithFormat:@"%@circle/dynamic/%@?page=%@&token=%@",MIDIApiRoot,self.ID,page,_apptoken];
    NSLog(@"圈子详情接口%@",self.detailsapi);
    
    
    [EdHttp get:self.detailsapi params:nil progress:^(id downloadProgress)
     {
        
    }
        success:^(id responseObject)
     {
         NSLog(@"刷新%@",responseObject);
     }
        failure:^(NSError *error)
     {
         NSLog(@"失败");
     }];
    
    
}


@end
